package com.quectel.core.module.dept.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.dept.dao.DeptPersonRoleResourceDao;
import com.quectel.core.module.dept.dto.DeptPersonRoleResourceDto;
import com.quectel.core.module.dept.entity.DeptPersonRoleResourceEntity;
import com.quectel.core.module.dept.service.DeptPersonRoleResourceService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2022/07/27 10:30
 */
@DubboService
public class DeptPersonRoleResourceServiceImpl implements DeptPersonRoleResourceService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DeptPersonRoleResourceServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> DEPT_PERSON_ROLE_RESOURCE_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "DEPT_PERSON_ROLE_RESOURCE_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<DeptPersonRoleResourceDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(DEPT_PERSON_ROLE_RESOURCE_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private DeptPersonRoleResourceDao deptPersonRoleResourceDao;


    @Override
    public void deleteByResourceId(Long resourceId) {
        deptPersonRoleResourceDao.delete(
                new LambdaQueryWrapper<DeptPersonRoleResourceEntity>()
                        .eq(DeptPersonRoleResourceEntity::getAccessResourceId, resourceId)
        );
    }

    @Override
    public void deleteByRoleId(Long deptRoleId) {
        deptPersonRoleResourceDao.delete(
                new LambdaQueryWrapper<DeptPersonRoleResourceEntity>()
                        .eq(DeptPersonRoleResourceEntity::getDeptPersonRoleId, deptRoleId)
        );
    }


    @Override
    public List<DeptPersonRoleResourceDto> selectByRoleId(Long deptRoleId) {
        List<DeptPersonRoleResourceEntity> entities = deptPersonRoleResourceDao.selectList(
                new LambdaQueryWrapper<DeptPersonRoleResourceEntity>()
                        .eq(DeptPersonRoleResourceEntity::getDeptPersonRoleId, deptRoleId)
        );
        return CopyUtils.copyList(entities, DeptPersonRoleResourceDto.class);
    }

    @Override
    public DeptPersonRoleResourceDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                DEPT_PERSON_ROLE_RESOURCE_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public DeptPersonRoleResourceDto selectById(Long id) {
        DeptPersonRoleResourceEntity entity = deptPersonRoleResourceDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, DeptPersonRoleResourceDto.class));
    }

    @Override
    public List<DeptPersonRoleResourceDto> queryList(Map<String, Object> params) {
        List<DeptPersonRoleResourceEntity> list = deptPersonRoleResourceDao.queryList(params);
        List<DeptPersonRoleResourceDto> result = CopyUtils.copyList(list, DeptPersonRoleResourceDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属社区等
     *
     * @param dto
     * @return
     */
    private DeptPersonRoleResourceDto paddingField(DeptPersonRoleResourceDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return deptPersonRoleResourceDao.queryTotal(params);
    }

    @Override
    public Long save(DeptPersonRoleResourceDto dto) {
        DeptPersonRoleResourceEntity entity = CopyUtils.copyObj(dto, DeptPersonRoleResourceEntity.class);
        entity.setId(Snowflake.nextId());
        deptPersonRoleResourceDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(DeptPersonRoleResourceDto dto) {
        DeptPersonRoleResourceEntity entity = CopyUtils.copyObj(dto, DeptPersonRoleResourceEntity.class);
        deptPersonRoleResourceDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(DeptPersonRoleResourceDto dto) {
        DeptPersonRoleResourceEntity entity = CopyUtils.copyObj(dto, DeptPersonRoleResourceEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, deptPersonRoleResourceDao);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id) {

        CLEAR_CACHE_FUNC.accept(selectById(id));

        deptPersonRoleResourceDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
